@extends('admin.layout')



@section('content')
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                用户管理
                <small>用户列表</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="/admin/index"><i class="fa fa-dashboard"></i>首页</a></li>
                <li><a href="#">用户列表</a></li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">快速查询</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">
						@if((session('info')))
							<div id = 'jg' class="callout callout-info">
                            <p>{{session('info')}}</p>
                              
                            </div>

                            @endif
                           <!-- select -->
							<form action="{{url('/admin/user/index')}}" method="get">
                           <div class="row">
                           		<div class="col-md-2">
                           			<div class="form-group">
	                        			<select name ="num" class="form-control">
	                                        <option
										@if(!empty($request['num']) && $request['num'] == 10)
										selected ="selected"
										@endif

	                                        >10</option>
	                                        <option
										@if(!empty($request['num']) && $request['num'] == 20)
										selected ="selected"
										@endif
	                                        >20</option>
	                                        <option
										@if(!empty($request['num']) && $request['num'] == 50)
										selected ="selected"
										@endif
	                                        >50</option>
	                                        <option
										@if(!empty($request['num']) && $request['num'] == 100)
										selected ="selected"
										@endif
	                                        >100</option>
	                                    </select>
	                                </div>

                           		</div>
                           
                           <div class="col-md-4 col-md-offset-6" >
                           		<div class="input-group">
                              		<input value ="{{$request['keywords'] or '' }}" type="text" name ="keywords" class="form-control">
				                    <span class="input-group-btn">
				                    <button class="btn btn-info btn-flat">搜索</button>
				                    </span>
				               	</div>

                           </div>
                         </div>   
						</form>

                            <table id="example2" class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th>用户ID</th>
                                    <th>用户名</th>
                                    <th>邮箱</th>
                                    <th>头像</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($data as $v)
                                <tr>
                                    <td class="ids">{{$v -> id}}</td>
                                    <td class="name">{{$v -> name}}</td>
                                    <td>{{$v -> email}}</td>
                                    <td>
									<img  width="50px"src="/uploads/yhtx
                                  /{{$v -> pic}}" ></td>
                                    <td class = "status">
                                    	@if($v -> status ==0)
                                    	禁用
										@elseif($v -> status == 1)
										启用
										@endif
                                    </td>
                                    <td><a href="{{
                                    url('/admin/user/edit/')}}/{{$v -> id }}"
                            		>编辑</a> <a href="{{
                                    url('/admin/user/delete/')}}/{{$v -> id }}"
                            		>删除</a></td>                      
                                </tr>
                               @endforeach
                                </tbody>
                             
                            </table>
							{{ $data ->appends($request) -> render() }}

                        </div>
                </div>
            </div>
        </section>
    </div>
    <script type="text/javascript">

    // 更改用户状态
    window.onload =function()
    {
    	$.ajaxSetup({
    		headers:{
    			'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content')
    		}
    	});

    	$('.status').on('click',function(){
    		//获取当前对象
    		var t = $(this);
    		//获取ID
    		var id = $(this).parent().find('.ids').html();

    		$.ajax({
    			type:"POST",
    			url:"{{url('/admin/user/ajaxStatus')}}",
    			data:{id:id},
    			success:function(data)
    			{
    				if(data ==2)
    				{
    					alert('修改失败')
    				}else if(data == 0)
    				{
    					t.html('禁用');
    				}else if(data == 1)
    				{
    					t.html('启用');
    				}
    			},
    			error:function()
    			{
    				alert('yichang');
    			}
    		});

    	});

    	//双击更改用户名

    	$('.name').on('dblclick',dblClick);
    	function dblClick(){
    		var id = $(this).parent().find('.ids').html();
    		//准备Inp
    		var inp =$("<input type='text' />");
			//获取原来的内容
			var oldName = $(this).html();
			// 将原来内容添加到input
			inp.val(oldName);
    		//修改td内容
    		$(this).html(inp);

			inp.select();

    		var t = $(this);

    		$(inp).on('blur',function(){
    			//获取新的inp内容
    			var newName = inp.val();

    			$.ajax({
    			type:"POST",
    			url:"{{url('/admin/user/ajaxName')}}",
    			data:{id:id,name:newName},
    			success:function(data)
    			{
    				if(data == 4)
    				{
    					alert('用户名存在');
    					t.html(oldName);
    				}else if(data == 0)
    				{	
    					alert('修改失败');
    					t.html(oldName);
    				}else if(data == 1)
    				{
    					t.html(newName);
    				}
    			},
    			error:function()
    			{
    				alert('yichang');
    			}
    		});
    			//注册事件
    			t.on('dblclick',dblClick);
    		});
    		t.unbind('dblclick')
    	}

    }


        window.onload = function()
        {
            setInterval(function(){
                $("#jg").fadeOut(1000);
            },3000);
        }
 
    </script>
@endsection()



